package view;

import dao.customerDao;
import model.Customer;
import util.dbUtil;
import util.strUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @program: foodApp
 * @description:
 * @author: PanKer
 * @create: 2021-11-20 11:39
 */

@WebServlet("/customerRegister")
public class customerRegister extends HttpServlet {
    private static final long serialVersionUID = -6889072270384972942L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        boolean registerCheck = false;


        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String province = strUtil.toUtf8(req.getParameter("address_province"));
        String city = strUtil.toUtf8(req.getParameter("address_city"));
        String address = province + city;
        String email = req.getParameter("email");
        String tel = req.getParameter("tel");
        String date = req.getParameter("date");

        Customer customer;
        Connection conn = null;
        try {
            conn = dbUtil.getConn();
            customer = new Customer(
                    username,
                    password,
                    address,
                    email,
                    tel,
                    date
            );

            System.out.println(
                    "用户名：" + username + "\n" +
                            "密码：" + password + "\n" +
                            "地址：" + address + "\n" +
                            "邮箱：" + email + "\n" +
                            "电话：" + tel + "\n" +
                            "出生日期：" + date
            );

            if (customerDao.isExist(conn, username)) {
                System.out.println("该用户名已存在！！！");
            } else {
                if (customerDao.add(conn, customer) > 0) {
                    System.out.println("新用户注册成功！！！");
                    registerCheck = true;
                } else System.out.println("新用户注册失败 qaq");
            }
        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException错误");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("SQLException错误");
            e.printStackTrace();
        } finally {
            try {
                System.out.println("数据库已关闭");
                conn.close();
            } catch (SQLException e) {
                System.out.println("数据库关闭失败");
                e.printStackTrace();
            }
        }

        if (registerCheck) {
            writer.println(
                    " <!DOCTYPE html> " + "\n" +
                            " <html lang='en'> " + "\n" +
                            "  " + "\n" +
                            " <head> " + "\n" +
                            " <meta charset='UTF-8'> " + "\n" +
                            " <meta http-equiv='X-UA-Compatible' content='IE=edge'> " + "\n" +
                            "                      <meta name='viewport' content='width=device-width, initial-scale=1.0'> " + "\n" +
                            "                       <link rel='stylesheet' href='https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css'> " + "\n" +
                            "                        <script src='https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js'></script>" + "\n" +
                            "                       <script src='https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js'></script> " + "\n" +
                            "                       <script src='https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js'></script>" + "\n" +
                            "                       <title>adminVerification</title> " + "\n" +
                            "                      <link rel='shortcut icon' href='img/Icon.ico' type='image/x-icon'> " + "\n" +
                            " </head> " + "\n" +
                            " <body>" + "\n" +
                            "<h1>好，你的已经账号已经注册成功了</h1>" + "\n" +
                            " </body>" + "\n" +
                            "</html>"

            );
        } else {
            System.out.println("注册失败了qaq");
            resp.sendRedirect(req.getContextPath() + "customer_register.html");
        }
    }
}
